home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / prgmming / swag05 / textfile.swg < prev    next >
Encoding:
Text File  |  1994-09-22  |  17.9 KB  |  1 lines

  1. SWAGOLX.EXE (c) 1993 GDSOFT  ALL RIGHTS RESERVED 00004                                                                           1      05-25-9408:23ALL                      ROWAN MCKENZIE           Text file position       SWAG9405            29     F╔   {π JK> I've started out in Pascal and need some information on howπ JK> to read from a certain point in a file, say line 3.  Howπ JK> would I set the pointer to line 3 to read into a variable?ππ BvG> A seek does not work on textfiles.ππ Here, this will assist you. originally in a Pascal Newsletter, so it mustπ be PD.ππ---------------------------------------- CUT HERE --------------------------π}πUnit TextUtl2;  (* Version 1.0 *)ππ{Lets you use typed-file operators on TEXT files.  Note that I've cut out MOST}π{of the documentation so as to make it more practical for the PNL.  I strongly}π{advise that you get in touch with the author at the address below (I haven't)}π{It's called TEXTUTL2 because it's a rewrite of an earlier unit called        }π{TEXTUTIL which had some nasty limitations.                                   }ππ{Both files can be FREQed from 3:634/384.0 as TEXTUT*.*, and I strongly       }π{recommend that you do so.                                                    }ππ{I tried looking up the author's telephone number, but Telecom says the number}π{is silent.  Oh well.                                                         }ππ{If you're having trouble, netmail me (Mitch Davis) at 3:634/384.6            }πππ(*πAuthor: Rowan McKenzie  28/12/88π        35 Moore Ave, Croydon, Vic, AustraliaππThese 3 routines are improvements to Tim Baldock's TEXTUTIL.PAS unit.πI can be contacted on: Eastwood, Amnet or Tardis BBS (Melbourne Australia)π*)ππInterfaceππUses Dos;ππProcedure TextSeek     (Var F : Text; Offset : Longint);πFunction  TextFileSize (Var F : Text): LongInt;πFunction  TextFilePos  (Var F : Text): LongInt;ππImplementationππProcedure TextSeek(Var F : Text; Offset : Longint);ππ{ seek char at position offset in text file f}ππvar BFile    : File of byte absolute F;  (* Set up File for Seek *)π    BFileRec : FileRec absolute Bfile;π    TFileRec : TextRec Absolute F;π    OldRecSize : Word;π    oldmode : word;ππBeginπ  With BfileRec do Beginπ    oldmode:=mode;π    Mode := FmInOut;         (* Change file mode so Turbo thinks it is *)π    OldRecSize := RecSize;   (* dealing with a untyped file.           *)π    RecSize := 1;            (* Set the Record size to 1 byte.         *)π    Seek(Bfile,Offset);      (* Perform Seek on untyped file.          *)π    Mode := oldmode;         (* Change file mode back to text so that  *)π    RecSize := OldRecSize;   (* normal text operation can resume.      *)π  end;π  TfileRec.BufPos := TfileRec.BufEnd; (* Force next Readln.              *)πend; {textseek}ππFunction TextFileSize(Var F : Text): LongInt;ππ{ determine size of text file f in bytes}ππvar BFile:File of byte absolute F;π    BFileRec:FileRec absolute Bfile;π    OldRecSize:Word;π    oldmode:word;ππBeginπ  With BfileRec do Beginπ    oldmode:=mode;π    Mode := FmInOut;π    OldRecSize := RecSize;π    RecSize := 1;π    TextFileSize := FileSize(Bfile);π    Mode := oldmode;π    RecSize := OldRecSize;π  end;πend; {textfilesize}πππFunction Textfilepos(Var F : Text): LongInt;ππ{ determine current position (in bytes) in text file f}ππvar BFile:File of byte absolute F;π    BFileRec:FileRec absolute Bfile;π    TFileRec:TextRec Absolute F;π    OldRecSize:Word;π    oldmode:word;ππBeginπ  With BfileRec do Beginπ    oldmode:=mode;π    Mode := FmInOut;π    OldRecSize := RecSize;π    RecSize := 1;π    textfilepos := Filepos(Bfile)-tfilerec.bufend+tfilerec.bufpos;π    Mode := oldmode;π    RecSize := OldRecSize;π  end;πend; {textfilepos}ππend.π                                                                                                 2      05-25-9408:23ALL                      WILBERT VAN LEIJEN       Positioning Text File    SWAG9405            21     F╔   πUnit TextUtil;π{ Written by Wilbert Van.Leijen and posted in the Pascal Echo }ππInterfaceππFunction TextFilePos(Var f : Text) : LongInt;πFunction TextFileSize(Var f : Text) : LongInt;πProcedure TextSeek(Var f : Text; n : LongInt);ππImplementationπuses Dos;ππ{$R-,S- }ππProcedure GetFileMode; Assembler;ππASMπ        CLCπ        CMP    ES:[DI].TextRec.Mode, fmInputπ        JE     @1π        MOV    [InOutRes], 104         { 'File not opened for reading' }π        XOR    AX, AX                  { Zero out function result }π        XOR    DX, DXπ        STCπ@1:πend;  { GetFileMode }ππFunction TextFilePos(Var f : Text) : LongInt; Assembler;ππASMπ        LES    DI, fπ        CALL   GetFileModeπ        JC     @1ππ        XOR    CX, CX                  { Get position of file pointer }π        XOR    DX, DXπ        MOV    BX, ES:[DI].TextRec.handleπ        MOV    AX, 4201hπ        INT    21h                     { offset := offset-BufEnd+BufPos }π        XOR    BX, BXπ        SUB    AX, ES:[DI].TextRec.BufEndπ        SBB    DX, BXπ        ADD    AX, ES:[DI].TextRec.BufPosπ        ADC    DX, BXπ@1:πend;  { TextFilePos }πππFunction TextFileSize(Var f : Text) : LongInt; Assembler;ππASMπ        LES    DI, fπ        CALL   GetFileModeπ        JC     @1π        XOR    CX, CX                  { Get position of file pointer }π        XOR    DX, DXπ        MOV    BX, ES:[DI].TextRec.handleπ        MOV    AX, 4201hπ        INT    21hπ        PUSH   DX                      { Save current offset on the stack }π        PUSH   AXπ        XOR    DX, DX                  { Move file pointer to EOF }π        MOV    AX, 4202hπ        INT    21hπ        POP    SIπ        POP    CXπ        PUSH   DX                      { Save EOF position }π        PUSH   AXπ        MOV    DX, SI                  { Restore old offset }π        MOV    AX, 4200hπ        INT    21hπ        POP    AX                      { Return result}π        POP    DXπ@1:πend;  { TextFileSize }ππProcedure TextSeek(Var f : Text; n : LongInt); Assembler;ππASMπ        LES    DI, fπ        CALL   GetFileModeπ        JC     @2ππ        MOV    CX, Word Ptr n+2        { Move file pointer }π        MOV    DX, Word Ptr nπ        MOV    BX, ES:[DI].TextRec.Handleπ        MOV    AX, 4200hπ        INT    21hπ        JNC    @1                      { Carry flag = reading past EOF }π        MOV    [InOutRes], AXπ        JMP    @2πππ        { Force read next time }π@1:     MOV    AX, ES:[DI].TextRec.BufEndπ        MOV    ES:[DI].TextRec.BufPos, AXπ@2:πend;  { TextSeek }πend.  { TextUtil }ππ    3      05-25-9408:24ALL                      KIMMO FREDRIKSON         Linking text file w/com..SWAG9405            19     F╔   {ππ     This is not related to the original topic ".. w/exe!!", butπ     if somebody is interested, at least I found this one a bitπ     excited piece of code. It makes an executable com-file fromπ     your text and you can easily extend it to the limits youπ     need. Just remember that you can't call any pascal routines,π     you have to write it in pure assembler. (would .80xxx have beenπ     a better area..?) Anyway, here it is:ππ --!clip!-- { Code by Kimmo Fredrikson }ππ  {$A+,D-,G-,I-,R-,S-}ππ  program txt2com;ππ  varπ    src                 : file;π    dst                 : file;π    buff                : array [0..2047] of byte;π    bytesRead           : word;π    bytesWritten        : word;π    fSize               : word;πππ  function t2c: word; far; assembler;π  asmπ        jmp     @tail           { 2 bytes }ππ  @head:mov     ax, 0003h       { -- here starts the code part of }π        int     10h             {    the txt-show-proggie.. }ππ        mov     cx, word ptr [@tail+100h-2]     { length of text }π        lea     si, [@tail+100h-2+2]            { address of txt }ππ  @nxtC:mov     dl, [si]        { read a character to dl }π        mov     ah, 2π        int     21hπ        inc     siπ        loop    @nxtCππ        mov     ax, 4c00hπ        int     21h             { terminate, back to dos }ππ  @tail:mov     ax, offset [@tail]              { length of t2c }π        sub     ax, offset [@head] { this returns the length of the  }π  end;                     { assembler code when called within this pascal }π                                                { program }π  beginπ    if paramCount <> 2 then halt;π    assign (src, paramStr (1));π    assign (dst, paramStr (2));π    reset (src, 1);π    if ioResult <> 0 then halt;π    if fileSize (src) > 64000 then halt;π    fSize := fileSize (src) - 1;                { get rid of the ctrl-z }π    reWrite (dst, 1);π    if ioResult <> 0 then halt;π    blockWrite (dst, pointer (longint (@t2c) + 2)^, t2c);  { the code }π    blockWrite (dst, fSize, 2);                  { the length of text }π    repeatπ      blockRead (src, buff, 2048, bytesRead);π      blockWrite (dst, buff, bytesRead, bytesWritten);     { the text }π    until (bytesRead = 0) or (bytesWritten <> bytesRead);π    close (src);π    close (dst);π  end.π    4      05-26-9406:18ALL                      SCOTT F. EARNEST         UNIX/Dos Text Converter  SWAG9405            73     F╔   {πI've gotten a couple requests for this source, which quasi-intelligentlyπconverts Unix-format text to DOS-format text and vice versa.  Recently,πI justπadded a better command-line interpreter, and cleaned it up a little.  I wasπhoping to get around to using untyped files instead of text files, but maybeπlater.ππThis is probably not the most graceful (and since it uses text files,πnot the fastest way to do this), but it's worked well for me.πSuggestions on how to improve are welcome.ππ-Scott E.πtiobe@cmu.eduπ------------------------------------------------------------------}πprogram SConvert;π π{Smart-converts UN*X/DOS format filesπ π Usage:  sconvert infile [outfile] [/U | /D]π          [/U forces unix, /D forces DOS, if forced type, do nothing.]ππ         -- or --π π         sconvert /?  (-?, /h, -h, /H, and -H analogous)π          for help messageπ π         This program is capabable of having its output piped, providedπ          it is the first in the pipeline.  Since it opens input twice,π          using it anywhere in a pipe besides the beginning probably won'tπ          work well.π π Written by Scott F. Earnest, Aug 1993π Original version:  30 Aug 1993π Updated version:    9 May 1994  (Added force flags.)π}π πuses Crt;π πconstπ  CR = chr(13);               {Carriage Return}π  LF = chr(10);               {Line Feed}π πtypeπ  sys = (dos,unix,bad);       {system identifier}π πvarπ  sysID : sys;                {system identifier for case branch}π  infile, outfile : string;   {input/output files}π  force : sys;                {What mode to work in.}π πfunction exist (filename : string) : boolean;π π{Check if a file exists or notπ returns:  true  -->  file existsπ           false -->  file non-existent}π πvarπ  openfile : text;π  errcode : integer;π πbeginπ  {$I-}                       {Turn off error-checking}π  assign (openfile, filename);π  reset (openfile);π  {$I+}                       {Turn it back on}π  errcode := IOResult;        {Get error code}π  if  errcode <> 0  then      {There's an error if non-zero}π    exist := false            {So flag that it doesn't exist.}π  elseπ    beginπ      close (openfile);       {Otherwise, close file}π      exist := true;          {Flag that it does exist}π    end;πend;ππfunction selectyn : boolean;π π{Get a yes/no single-keypress responseπ returns:  true  -->  yes response, y or Yπ           false -->  no response, n or N}π πvarπ  getchar : char;             {Need something to read into}π πbeginπ  while KeyPressed do         {Clean keyboard buffer}π    getchar := ReadKey;π  repeat                      {Get a key until it's a (Y)es or (N)o.}π    getchar := ReadKey;π    getchar := upcase (getchar);π  until (getchar in ['Y', 'N']);π  writeln (getchar);          {Print the response}π  case getchar of             {Tell it what it should return}π    'Y' : selectyn := true;π    'N' : selectyn := false;π  end;πend;π πprocedure help (badflag : boolean);π π{brief message if command format was abused}π πbeginπ  writeln ('SmartConvert, Written by Scott F. Earnest -- v1.3 -- 9 May 1994');π  writeln;π  if badflag thenπ    beginπ      writeln ('Invalid flag.');π      writeln;π    end;π  writeln ('Usage');π  writeln ('  sconvert infile [outfile] [/d | /u]');π  writeln;π  writeln ('Use /d to force conversion to DOS, and /u to force UNIX.');π  halt (1);πend;π πprocedure incheck (filename : string);π π{Make sure source exists, if specified}π πbeginπ  if not (exist (filename)) thenπ    beginπ      writeln ('Source file does not exist!');π      halt (3);π    end;πend;π πprocedure outcheck (filename : string);π π{Make sure target does NOT exist, if specified, allow overwrite}π πvarπ  select : boolean;π πbeginπ  if exist (filename) and (filename <> '') thenπ    beginπ      write ('Target file exists!  Overwrite?  [y/n] ');π      select := selectyn;π      case select ofπ        true : ;π        false : halt (4);π      end;π    end;πend;π πfunction checktype (readfile : string) : sys;π πvarπ  FileCheck : text;π  checkvar : sys;π  CROk, LFOk : boolean;π  ReadBuf : char;π πbeginπ  CROk := False;π  LFOk := False;                        {Init flags.}π  checkvar := bad;                      {Assume that type isn't known.}π  assign (FileCheck, readfile);π  reset (FileCheck);π  while (not eof(FileCheck)) and (not CROk) and (not LFOk) doππ    begin                               {Look for CR or LF}π      read (FileCheck, ReadBuf);π      if ReadBuf = CR then              {CR found?}π        beginπ          CROk := True;                 {If yes, set the CR flag.}π          Read (FileCheck, ReadBuf);    {and get next char}π          if ReadBuf = LF then          {next one a LF?}π            LFOk := True;               {Flag it as found.}π          if CROk and LFOk then         {So is it CR/LF?}π             beginπ               checktype := dos;        {If yes, specify DOS, and exit.}π               close (FileCheck);π               exit;π             end;π        end;π      if ReadBuf = LF then              {Found a LF?}π         beginπ           checktype := unix;           {If yes, assume unix.}π           close (FileCheck);           {Close and exit.}π           exit;π         end;π    end;π  if checkvar = bad then                {If there was a problem:}π    beginπ      writeln ('Ambiguous file type.  Can''t determine type.');π      close (FileCheck);π      halt(2);π    end;πend;π πprocedure dos2unix (infile, outfile : string);π πvarπ  intext, outtext : text;π  ReadBuf1, ReadBuf2 : char;π πbeginπ  writeln ('Converting DOS -> UNIX. . . .');π  assign (intext, infile);π  reset (intext);π  assign (outtext, outfile);π  rewrite (outtext);π  while not eof(intext) doπ    beginπ      read (intext, ReadBuf1);          {Get character}π      if ReadBuf1 = CR then             {If it's CR then. . . }π        beginπ          read (intext, ReadBuf2);      {. . . get next . . .}π          if ReadBuf2 = LF then         {. . . and see if it's LF.}π            write (outtext, LF)         {If yes, just put LF into new file.}π          elseπ            write (outtext, ReadBuf1, ReadBuf2); {Not CR/LF, dump to file.}π        endπ      elseπ        write (outtext, ReadBuf1);      {Dump the character to file.}π    end;π  close (intext);π  close (outtext);πend;π πprocedure unix2dos (infile, outfile : string);π πvarπ  intext, outtext : text;π  ReadBuf : char;π πbeginπ  writeln ('Converting UNIX -> DOS. . . .');π  assign (intext, infile);π  reset (intext);π  assign (outtext, outfile);π  rewrite (outtext);π  while not eof(intext) doπ    beginπ      read (intext, ReadBuf);           {Get a character.}π      if ReadBuf = LF then              {Is it LF?}π        write (outtext, CR+LF)          {If yes, put a CR/LF in its place.}π      elseπ        write (outtext, ReadBuf);       {Otherwise, replace the character.}π    end;π  close (intext);π  close (outtext);πend;π πprocedure getcommandline;π π{get commandline info. . . .}π πvarπ  pnum : byte;                          {paramater counter}π  pstr : string[2];                     {string snippet}π  fname : string;                       {temporary string}π πbeginπ  if (paramcount < 1) or (paramcount > 3) thenπ    help (false);                       {too few, too many--show help}π  infile := '';                         {Init names.}π  outfile := '';π  force := bad;π  for pnum := 1 to paramcount do        {Do this in two passes.}π    begin                               {#1.)  Flags}π      pstr := paramstr(pnum);           {Get parameter.}π      pstr[2] := upcase(pstr[2]);π      if pstr[1] in ['-', '/'] then     {Flag?}π        case pstr[2] of  π          'H', '?' : help (false);      {Is help.}π          'D'      : force := dos;      {Is force DOS.}π          'U'      : force := unix;     {Is force UNIX.}π        elseπ          help (true);                  {Bad switch.}π        end;π    end;π  for pnum := 1 to paramcount do        {#2.)  Filenames}π    begin  π      fname := paramstr(pnum);          {Get parameter.}π      if not (fname[1] in ['-', '/']) thenπ        begin                           {If not flag then}π          if infile = '' then           {Get infile}π            infile := fnameπ          else if (infile <> '') and (outfile = '') thenπ            outfile := fname            {Get outfile}π          elseπ            help (false);               {Oops, too many.}π        end;π    end;πend;π πbeginπ  getcommandline;                       {Parse parameters}π  sysID := checktype (infile);          {Check the input file type}π  if sysID = force then                 {If it's getting forced, then}π    begin                               {compare types and skip if same.}π      write ('Input file is already type ');π      case sysID ofπ        dos  : write ('DOS');π        unix : write ('UNIX');π      end;π      writeln (', skipped.');π      halt(5);π    end;π  case sysID ofπ    dos : dos2unix (infile, outfile);    {DOS -> UNIX}π    unix : unix2dos (infile, outfile);   {UNIX -> DOS}π    bad : begin                          {Not likely to happen but. . . .}π            writeln ('Internal error!  Check source code and recompile.');π            halt (6);π          end;π  end;πend.π